package top.yueyazhui.word_freemarker.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.yueyazhui.word_freemarker.model.Favorite;
import top.yueyazhui.word_freemarker.model.Info;
import top.yueyazhui.word_freemarker.service.IExportDocService;
import top.yueyazhui.word_freemarker.util.ExportDocUtil;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/export")
public class ExportController {

    @Autowired
    IExportDocService exportDocService;
    @Autowired
    ExportDocUtil exportDocUtil;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public Info mock() throws ParseException {
        Info info = new Info();
        info.setId(1L);
        info.setName("月牙坠");
        info.setSex("男");
        info.setBirth(sdf.parse("1995-09-17"));
        info.setIDCard("******19950917****");
        info.setPhone("15235032479");

        // http://47.95.1.150/group1/M00/00/00/L18BlmLfrQqAHiBAAAAUVkRTotI522.jpg
        // http://47.95.1.150/group1/M00/00/00/L18BlmLfsh2ANk6yAASFyIGTnok085.jpg
        // http://39.99.231.76/group1/M00/00/12/J2PnTGLfuXSAdndKAAKK1Ca_FTk555.png
        // http://47.95.1.150/group1/M00/00/00/L18BlmJ3VoWAAoGWAAItxHZKwEI221.gif
        info.setAvatar("http://47.95.1.150/group1/M00/00/00/L18BlmJ3VoWAAoGWAAItxHZKwEI221.gif");

        List<Favorite> list = new ArrayList<>();
        Favorite favorite01 = new Favorite();
        favorite01.setId(1L);
        favorite01.setAuthor("步非烟");
        favorite01.setCartoon("墓王之王");
        favorite01.setSinger("许嵩");
        Favorite favorite02 = new Favorite();
        favorite02.setId(2L);
        favorite02.setAuthor("天下归元");
        favorite02.setCartoon("天行九歌");
        favorite02.setSinger("邓紫棋");
        list.add(favorite01);
        list.add(favorite02);
        info.setFavorites(list);
        return info;
    }

    @GetMapping("/doc")
    public void exportDoc(HttpServletResponse response) throws Exception {

        Info info = mock();

        Map<String,Object> data = new HashMap<>();
        data.put("name", info.getName());
        data.put("sex", info.getSex());
        data.put("birth", sdf.format(info.getBirth()));
        data.put("IDCard", info.getIDCard());
        data.put("phone", info.getPhone());
        data.put("avatar", exportDocUtil.getImageBase64(info.getAvatar()));
        data.put("favorites", info.getFavorites());

        // 文件名称
        String fileName = "个人信息.doc";
        exportDocService.exportDocToClient(response, fileName, "info.html", data);

//        String fileName = "个人信息.xls";
//        exportDocService.exportDocToClient(response, fileName, "info1.html", data);
    }
}
